import BaseNativeAd from "../../../sdk/base/BaseNativeAd";
import SDKManager from "../../../sdk/SDKManager";
import BaseNativeAdItemModel from "../../../sdk/base/BaseNativeAdItemModel";
import BaseView from "../../../cfw/mvc/BaseView";
import UIManager from "../../../cfw/ui/UIManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SNativeAdView extends BaseView {

    @property({ type: cc.Button, displayName: "back$VButton" })
    back$VButton: cc.Button = null;
    @property(cc.Sprite)
    icon: cc.Sprite = null;

    protected model:BaseNativeAdItemModel;
    start() {
        let nativeAd: BaseNativeAd = SDKManager.getChannel().getNativeAd()
        if (nativeAd) {
            this.node.active = true;
            SDKManager.getChannel().showNativeAd(0, (modelList: BaseNativeAdItemModel[]) => {
                if (modelList.length > 0) {
                    this.model = modelList[0]
                    SDKManager.getChannel().reportAdShow(this.model.getID())
                    let icon = this.model.getIcon()
                    if (icon) {
                        cc.assetManager.loadRemote(icon, (err, res: cc.Texture2D) => {
                            if (err || !cc.isValid(this.node)) {
                                return;
                            }
                            this.icon.spriteFrame = new cc.SpriteFrame(res)
                        })
                    }
                } else {
                    this.hide()
                }

            })
        } else {
            this.node.active = false
        }
    }
    
    onback$VButtonClick() {
        this.node.active = false;
    }

    onNativeAdClick() {
        if (this.model) {
            SDKManager.getChannel().reportAdClick(this.model.getID())

        }
    }
    // update (dt) {}
}
